﻿using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;

/// <summary>
/// Summary description for ServiceLib
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class ServiceLib : System.Web.Services.WebService
{
    public ServiceLib()
    {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public AjaxControlToolkit.CascadingDropDownNameValue[] GetListOfStates(string knownCategoryValues)
    {
        List<AjaxControlToolkit.CascadingDropDownNameValue>
                value = new List<AjaxControlToolkit.CascadingDropDownNameValue>();

        try
        {
            GssDO.DOState DO = new GssDO.DOState();
            DataTable dt = (DataTable)HttpContext.Current.Cache["ListOfState"];

            if (dt == null)
            {
                GssBL.BLState BL = new GssBL.BLState();
                dt = BL.getAllDataNotDeleted();
                HttpContext.Current.Cache.Insert("ListOfState", dt);
            }

            foreach (DataRow dr in dt.Rows)
            {
                value.Add(new AjaxControlToolkit.CascadingDropDownNameValue(dr[DO.TitleColumnName].ToString(), dr[DO.PKIDStateColumnName].ToString()));
            }

            string id = "";
            if (HttpContext.Current.Cache["IDState"] != null)
                id = HttpContext.Current.Cache["IDState"].ToString();

            for (int i = 0; i < value.Count; i++)
                if (value[i].value == id)
                {
                    value[i].isDefaultValue = true;
                    break;
                }
        }
        catch (Exception exc)
        {
            string mess = exc.Message;
        }
        return value.ToArray();
    }

    [WebMethod]
    public AjaxControlToolkit.CascadingDropDownNameValue[] GetListOfLocationsByState(string knownCategoryValues)
    {
        StringDictionary kv = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        List<AjaxControlToolkit.CascadingDropDownNameValue>
                    value = new List<AjaxControlToolkit.CascadingDropDownNameValue>();
        try
        {
            if (kv.ContainsKey("IDState") == false)
            {
                return null;
            }

            string idState = "";
            GssDO.DOLocation DO = new GssDO.DOLocation();
            GssBL.BLLocation BL = new GssBL.BLLocation();

            IEnumerator enu = kv.Values.GetEnumerator();
            if (enu.MoveNext())
                idState = enu.Current.ToString();

            DataTable dt = BL.getLocationByState(idState);

            foreach (DataRow dr in dt.Rows)
            {
                value.Add(new AjaxControlToolkit.CascadingDropDownNameValue(dr[DO.TitleColumnName].ToString(), dr[DO.PKIDLocationColumnName].ToString()));
            }

            string id = "";
            if (HttpContext.Current.Cache["IDLocation"] != null)
                id = HttpContext.Current.Cache["IDLocation"].ToString();

            for (int i = 0; i < value.Count; i++)
                if (value[i].value == id)
                {
                    value[i].isDefaultValue = true;
                    break;
                }
        }
        catch (Exception exc)
        {
            string mess = exc.Message;
        }

        return value.ToArray();
    }

}

